<template>
  <!-- 头部区域 -->
  <el-container class="home-container">
    <el-header>
      <div>
        <img class="logo" src="../assets/images/logo.png" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 展开折叠按钮 -->
        <div class="toggleButton" :class="{ active: isCollapse }" @click="toggleCollapse">
          <span>|||</span>
        </div>
        <!-- 侧边栏菜单区域 -->
        <!-- 在提供布尔值的时候记得在属性前面添加冒号，可以将这个属性变成动态的值也就是，将值变成表达式 -->
        <el-menu
          background-color="#F0F0F0"
          text-color="#000"
          active-text-color="rgb(222, 96, 11)"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <!-- 鼠标点击展开的时候是根据index的值进行展开的，如果不修改成动态的值就会导致，鼠标点击所有的菜单都展开。注意：index只接受字符串 -->
          <el-submenu :index="submenu.id + ''" v-for="submenu in menuList" :key="submenu.id">
            <!-- 一级菜单内容区域 -->
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i :class="iconObj[submenu.id]"></i>
              <!-- 一级菜单文本 -->
              <span>{{ submenu.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + item.path"
              v-for="item in submenu.children"
              :key="item.id"
              @click="setNavStatus('/' + item.path)"
            >
              <template slot="title">
                <!-- 二级菜单图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单文本 -->
                <span>{{ item.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created () {
    // 获取表单列表
    this.getMenusList()
    // 获取保存在session中的历史路径
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      // 表单列表
      menuList: [],
      // 图标对象，用于设置图标
      iconObj: {
        // eslint-disable-next-line quote-props
        '125': 'iconfont icon-users',
        // eslint-disable-next-line quote-props
        '103': 'iconfont icon-tijikongjian',
        // eslint-disable-next-line quote-props
        '101': 'iconfont icon-shangpin',
        // eslint-disable-next-line quote-props
        '102': 'iconfont icon-danju',
        // eslint-disable-next-line quote-props
        '145': 'iconfont icon-baobiao'
      },
      // 是否展开菜单列表
      isCollapse: false,
      // 当前被激活的二级菜单path
      activePath: ''
    }
  },
  methods: {
    // 退出功能
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 异步获取菜单列表
    async getMenusList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$message.error('菜单列表读取失败')
      }
      // 合并菜单列表
      this.menuList = this.menuList.concat(res.data)
    },
    // 切换展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    setNavStatus (activePath) {
      // 将当前点击的二级菜单的路劲保存在session中
      window.sessionStorage.setItem('activePath', activePath)
      // 设置data中activePath来改变属性default-active
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    background-color: #303030;
    padding-left: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      display: flex;
      align-items: center;
      .logo {
        width: 50px;
      }
      span {
        font-size: 30px;
        margin-left: 15px;
        color: #fff;
      }
    }
  }
  .el-aside {
    transition: width 0.35s ease-in-out;
    background-color: #f0f0f0;
    .toggleButton {
      width: 100%;
      height: 30px;
      background-color: rgba(128, 128, 128, 0.61);
      line-height: 30px;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
      transition: background-color 0.3s;
      &:hover {
        background-color: rgb(128, 128, 128);
      }
      &.active {
        width: 100%;
        height: 30px;
        background-color: rgba(128, 128, 128, 0.61);
        line-height: 30px;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
        transition: background-color 0.3s;
        span {
          display: inline-block;
          transform: rotate(90deg);
          animation: transform duration timing-function delay;
        }
        &:hover {
          background-color: rgb(128, 128, 128);
        }
      }
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #fff;
  }
  .iconfont {
    padding-right: 10px;
  }
}
</style>
